package ware.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feign.utils.R;
import com.feign.valid.AddGroup;
import com.feign.valid.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import ware.domain.PurchaseDetail;
import ware.service.PurchaseDetailService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * (PurchaseDetail)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:11:29
 */
@RestController
@RequestMapping("ware/purchaseDetail")
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @GetMapping("list")
    public R list(@RequestParam Map<String, Object> params) {
        Page<PurchaseDetail> purchaseDetailPage = purchaseDetailService.querPage(params);
        return R.ok().put("data", purchaseDetailPage);
    }

    /**
     * 删除采购项
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R remove(@RequestBody Long[] ids) {
        boolean result = purchaseDetailService.removeBatchByIds(List.of(ids));
        return result ? R.ok("删除采购项成功") : R.error("删除采购项失败");
    }


    /**
     * 增加采购项
     *
     * @param purchaseDetail
     * @return
     */
    @PostMapping("save")
    public R save(@Validated({AddGroup.class}) @RequestBody PurchaseDetail purchaseDetail) {
        boolean result = purchaseDetailService.save(purchaseDetail);
        return result ? R.ok("增加采购项成功") : R.error("增加采购项失败");
    }

    /**
     * 修改采购项
     *
     * @param purchaseDetail
     * @return
     */
    @PutMapping("update")
    public R edit(@Validated({UpdateGroup.class}) @RequestBody PurchaseDetail purchaseDetail) {
        boolean result = purchaseDetailService.updateById(purchaseDetail);
        return result ? R.ok("修改采购项成功") : R.error("修改采购项失败");
    }


    /**
     * 获取单个采购项的数据
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    public R info(@PathVariable Long id) {
        PurchaseDetail purchaseDetail = purchaseDetailService.getById(id);
        return R.ok().put("data", purchaseDetail);
    }


}

